package utils.utilcode.blankj.com.myapplication;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import utils.utilcode.blankj.com.myapplication.view.productInfo;

import static java.lang.String.format;

/**
 * Created by Administrator on 2017-12-06.
 */

public class productInfoAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<productInfo> productInfoList;
    private LayoutInflater layoutInflater;
    private int i;
    //检测添加的菜单，防止回来时被隐藏
    private List<Integer> list = new ArrayList<>();
    private float[] mCurrentPosition = new float[2];
    public int mLastposition = -100;
    private SparseArray<Integer> lastnumberArray = new SparseArray();
    private Product mProductNumber;
    Integer mInteger;

    //boolean定义点击接口
    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void clearData() {
        for (int i = 0; i < lastnumberArray.size(); i++) {
            int key = lastnumberArray.keyAt(i);
            lastnumberArray.put(key, 0);
        }
        //将点击增加的集合清空，所以会执行onBindView时会隐藏
        list.clear();
        //从0开始自增
        i = 0;
        notifyItemRangeChanged(0, productInfoList.size());
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 1;
        } else {
            return 0;
        }

    }

    public productInfoAdapter(Context context, List<productInfo> productInfoList) {
        this.productInfoList = productInfoList;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 1) {
            return new HeadViewVH(layoutInflater.inflate(R.layout.item_headviewvh, parent, false));

        } else {
            return new NormalViewHolder(layoutInflater.inflate(R.layout.item_normalviewholder, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        switch (getItemViewType(position)) {
            case 1:
                HeadViewVH secondViewHolder = (HeadViewVH) holder;
                secondViewHolder.tv_headview_meue.setText("菜单");
                break;
            case 0:
                final NormalViewHolder thirdViewHolder = (NormalViewHolder) holder;
//                thirdViewHolder.thirdCategory.setText(lists.get(position).getCategoryName());
                thirdViewHolder.add.setOnClickListener(new View.OnClickListener() {


                    @Override
                    public void onClick(View view) {
                        boolean isPositionChange = false;
                        boolean hasChoice = false;
                        view.setTag(1);
                        if (!list.contains(position - 1)) {
                            list.add(position - 1);

                        }
//                        Log.e("productInfoAdapter","pos   "+position);
                        //与上次点击的位置不同，同时将上次的次数保存起来
//                        Log.e("productInfoAdapter", "mLastposition " + mLastposition);
//                        Log.e("productInfoAdapter", "position - 1  "+(position - 1));

                        if (mLastposition != position - 1) {
                            isPositionChange = true;
                            Log.e("productInfoAdapter", i + "");
                            i = 0;

                        }
                        thirdViewHolder.jian.setVisibility(View.VISIBLE);
                        thirdViewHolder.number.setVisibility(View.VISIBLE);
                        ++i;

                        //获取上次对应位置的数字
                        if (isPositionChange && !hasChoice) {
                            hasChoice = true;
                            mInteger = lastnumberArray.get(position - 1);
                            if (mInteger == null) {
                                mInteger = 0;
                            }
                        }

                        Log.e("productInfoAdapter", "mInteger  " + mInteger);
                        if (mInteger != null) {
                            thirdViewHolder.number.setText(String.format("%d", mInteger + i));
                            //把当前位置和数字存起来
                            lastnumberArray.put(position - 1, mInteger + i);

                            if (mProductNumber != null) {
                                //回调出去
                                mProductNumber.product(position - 1, mInteger + i);
                                mProductNumber.productPosition((ImageView) view);
                            }
                        }
//                        Log.e("productInfoAdapter", "保存的位置:" + (position - 1) + " 数值" + (mInteger + i));


                        if (mOnItemClickLitener != null)
                            mOnItemClickLitener.onItemClick(view, position - 1);

                        mLastposition = position - 1;
                    }


                });
                thirdViewHolder.jian.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        --i;
                        int number = lastnumberArray.get(position - 1);
                        --number;

                        thirdViewHolder.number.setText(String.format("%d", number));
                        if (number == 0) {
                            Iterator<Integer> it = list.iterator();
                            while (it.hasNext()) {
                                if (position == it.next() + 1) {
                                    it.remove();
                                }
                            }
                            thirdViewHolder.jian.setVisibility(View.GONE);
                            thirdViewHolder.number.setVisibility(View.GONE);
                        }
                        if (mOnItemClickLitener != null)
                            mOnItemClickLitener.onItemClick(view, position);
                        Log.e("productInfoAdapter", "减保存的位置" + (position - 1) + "数值 " + number);

                        lastnumberArray.put(position - 1, number);

                    }
                });
                if (list.contains(position - 1)) {
//                    lastnumberArray.put(position - 1, mInteger + i);
                    //点击后的效果
                    thirdViewHolder.number.setVisibility(View.VISIBLE);
                    thirdViewHolder.number.setText("" + lastnumberArray.get(position - 1));
                    thirdViewHolder.jian.setVisibility(View.VISIBLE);
                } else {
                    //没有点击的效果
                    thirdViewHolder.jian.setVisibility(View.GONE);
                    thirdViewHolder.number.setVisibility(View.GONE);
                }

                //利用view的Tag防止复用导致的错乱
//                if (thirdViewHolder.add.getTag() != null) {
//                    if ((int) thirdViewHolder.add.getTag() == 1) {
//                        //集合里面没有这些的话才隐藏
//                        //第一次选择的就不隐藏，复用的就隐藏
//
//                        if (!list.contains(position - 1)) {
//                            for (Integer integer : list) {
//                                Log.e("productInfoAdapter1","集合里面的位置+"+integer);
//
//                            }
//                            Log.e("productInfoAdapter1","隐藏的position+"+(position - 1));
//
//                            thirdViewHolder.jian.setVisibility(View.GONE);
//                            thirdViewHolder.number.setVisibility(View.GONE);
//                        } else {
//                            thirdViewHolder.jian.setVisibility(View.VISIBLE);
//                            thirdViewHolder.number.setVisibility(View.VISIBLE);
//                        }
//
//                    }
//                }


//
//                Integer integer = lastnumberArray.get(position - 1);
//                if(integer!=null){
//                    thirdViewHolder.jian.setVisibility(View.GONE);
//                    thirdViewHolder.number.setVisibility(View.GONE);
//                }
//                Log.e("productInfoAdapter", "position "+position);

                thirdViewHolder.name.setText(productInfoList.get(position - 1).name);
                thirdViewHolder.money.setText(format("%s", productInfoList.get(position - 1).money));
                break;

        }

    }

    private boolean hasChoosed(int i) {
        return list.contains(i);
    }


    @Override
    public int getItemCount() {
        return productInfoList.size() + 1;
    }

    public static class HeadViewVH extends RecyclerView.ViewHolder {
        TextView tv_headview_meue;

        public HeadViewVH(View itemView) {
            super(itemView);
            //headView
            tv_headview_meue = (TextView) itemView.findViewById(R.id.tv_headview_meue);
        }
    }

    public static class NormalViewHolder extends RecyclerView.ViewHolder {

        TextView name, money, number;
        ImageView jian, add;

        public NormalViewHolder(View itemView) {
            super(itemView);
            //面包
            name = (TextView) itemView.findViewById(R.id.name);
            //单价
            money = (TextView) itemView.findViewById(R.id.money);
            //减号
            jian = (ImageView) itemView.findViewById(R.id.jian);
            //加号
            add = (ImageView) itemView.findViewById(R.id.add);
            //购买的数量
            number = (TextView) itemView.findViewById(R.id.number);
        }

    }

    public interface Product {
        void product(int position, int number);

        void productPosition(ImageView imageView);
    }

    public void setProductNumber(Product mProductNumber) {
        this.mProductNumber = mProductNumber;
    }
}
